//
//  ICRedPacketPayPasswordView.m
//  ICome
//
//  Created by 席鹏飞 on 2016/11/10.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICRedPacketPayPasswordView.h"
#import "ICPasswordInputView.h"
#import "NSMutableAttributedString+Extension.h"
#import "ICHeaderFile.h"

@interface ICRedPacketPayPasswordView () <UITextFieldDelegate>
/** 密码框背景视图 */
@property (nonatomic, strong) ICPasswordInputView *passwordInputView;

@end

@implementation ICRedPacketPayPasswordView

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (instancetype)init {
    self = [super init];
    if (self) {
        self.frame = App_RootCtr.view.frame;
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        [self reSetPasswordInputViewFrame];
        
        /** 注册键盘通知 */
        [self setupKeyboardNotification];
        
        /** 回调block */
        [self callbackBlock];
    }
    return self;
}

- (void)reSetPasswordInputViewFrame {
    if (APP_Frame_Height == 736) {
        CGRect rect = self.passwordInputView.frame;
        rect.size.height = App_Frame_Width / 375.0 * rect.size.height;
        self.passwordInputView.frame = rect;
    }
}

/** 回调block  */
- (void)callbackBlock {
    WEAKSELF
    self.passwordInputView.closeBlock = ^(){
        [weakSelf close];
    };
    self.passwordInputView.forgetPWDBlock = ^(){
        if ([weakSelf.delegate respondsToSelector:@selector(forgetPWDBtnClicked)]) {
            [weakSelf.delegate forgetPWDBtnClicked];
        }
    };
    self.passwordInputView.doneBlock = ^(NSString *password){
        if ([weakSelf.delegate respondsToSelector:@selector(doneBtnClicked:)]) {
            [weakSelf.delegate doneBtnClicked:password];
        }
    };
}

/** 注册键盘通知 */
- (void)setupKeyboardNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

// 键盘出现通知
- (void)keyboardWillShow:(NSNotification *)notification {
    // 获取键盘的高度
    NSDictionary *userInfo = [notification userInfo];
    NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];
    CGFloat keyboardH = keyboardRect.size.height;
    
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionTransitionNone animations:^{
        self.passwordInputView.y = 20 + (APP_Frame_Height - keyboardH - self.passwordInputView.height - 20) * 0.5;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)showTotalWorkPoint {
    NSMutableAttributedString *workPoint = [NSMutableAttributedString attributedString:@[@"总计：", _totalPoint, @" 工分"] colors:@[ICRGB(0x333333), ICRGB(0x333333), ICRGB(0x333333)] fonts:@[[UIFont systemFontOfSize:11], [UIFont systemFontOfSize:14], [UIFont systemFontOfSize:11]]];
    self.passwordInputView.workPointLabel.attributedText = workPoint;
}

- (void)showInView:(UIView *)view
{
    [self showTotalWorkPoint];
    [view addSubview:self];
    /** 弹出键盘 */
    [self showKeyboard];
}

/** 输入框的关闭按钮点击 */
- (void)close {
    [self hideKeyboard:^(BOOL finished) {
        self.inputView.hidden = YES;
        [self removeFromSuperview];
        [self.inputView setNeedsDisplay];
    }];
}

/** 输入框的关闭按钮点击 */
- (void)clearThePassword {
    [self.passwordInputView clearThePassword];
}

#pragma mark - layout

- (void)layoutSubviews {
    [super layoutSubviews];
}

#pragma mark - 私有方法

/** 键盘弹出 */
- (void)showKeyboard {
    [self.passwordInputView.txfResponsder becomeFirstResponder];
}

/** 键盘退下 */
- (void)hideKeyboard:(void (^)(BOOL finished))completion {
    [self.passwordInputView.txfResponsder endEditing:NO];
    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.inputView.transform = CGAffineTransformIdentity;
    } completion:completion];
}

#pragma mark - Getter

- (ICPasswordInputView *)passwordInputView {
    if (!_passwordInputView) {
        _passwordInputView = [[ICPasswordInputView alloc] initWithFrame:CGRectMake((App_Frame_Width - 270) * 0.5, APP_Frame_Height, 270, 215)];
        [self addSubview:_passwordInputView];
    }
    return _passwordInputView;
}

@end
